<?php

namespace extend\wechat\mp;



/**
 * Class WechatLocation
 * @package App\Helpers\Wechat
 */
class Location
{

    /**
     *  WGS84转GCj02(北斗转高德)
     * @param $lng
     * @param $lat
     * @return array|mixed
     */
    public static function wgs84togcj02($lng, $lat)
    {
        $PI = 3.1415926535897932384626;
        $a = 6378245.0;
        $ee = 0.00669342162296594323;
        if (self::out_of_china($lng, $lat)) {
            return array($lng, $lat);
        } else {
            $dlat = self::transformlat($lng - 105.0, $lat - 35.0);
            $dlng = self::transformlng($lng - 105.0, $lat - 35.0);
            $radlat = $lat / 180.0 * $PI;
            $magic = sin($radlat);
            $magic = 1 - $PI * $magic * $magic;
            $sqrtmagic = sqrt($magic);
            $dlat = ($dlat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtmagic) * $PI);
            $dlng = ($dlng * 180.0) / ($a / $sqrtmagic * cos($radlat) * $PI);
            $mglat = $lat + $dlat;
            $mglng = $lng + $dlng;
            return self::bd_encrypt($mglng, $mglat);
        }
    }

    /**
     * @param $lng
     * @param $lat
     * @return float|int
     */
    public static function transformlat($lng, $lat)
    {
        $PI = 3.1415926535897932384626;

        $ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * sqrt(abs($lng));
        $ret += (20.0 * sin(6.0 * $lng * $PI) + 20.0 * sin(2.0 * $lng * $PI)) * 2.0 / 3.0;
        $ret += (20.0 * sin($lat * $PI) + 40.0 * sin($lat / 3.0 * $PI)) * 2.0 / 3.0;
        $ret += (160.0 * sin($lat / 12.0 * $PI) + 320 * sin($lat * $PI / 30.0)) * 2.0 / 3.0;
        return $ret;
    }


    /**
     * @param $lng
     * @param $lat
     * @return float|int
     */
    public static function transformlng($lng, $lat)
    {
        $PI = 3.1415926535897932384626;

        $ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * sqrt(abs($lng));
        $ret += (20.0 * sin(6.0 * $lng * $PI) + 20.0 * sin(2.0 * $lng * $PI)) * 2.0 / 3.0;
        $ret += (20.0 * sin($lng * $PI) + 40.0 * sin($lng / 3.0 * $PI)) * 2.0 / 3.0;
        $ret += (150.0 * sin($lng / 12.0 * $PI) + 300.0 * sin($lng / 30.0 * $PI)) * 2.0 / 3.0;
        return $ret;
    }

    /**
     * 判断是否在国内，不在国内则不做偏移
     * @param $lng
     * @param $lat
     * @return bool
     */
    public static function out_of_china($lng, $lat)
    {
        return ($lng < 72.004 || $lng > 137.8347) || (($lat < 0.8293 || $lat > 55.8271) || false);
    }


    //GCJ-02(火星，高德)坐标转换成BD-09(百度)坐标
    //@param bd_lon 百度经度
    //@param bd_lat 百度纬度
    /**
     * @param $gg_lon
     * @param $gg_lat
     * @return mixed
     */
    public static function bd_encrypt($gg_lon, $gg_lat)
    {
//        $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
//        $x = $gg_lon;
//        $y = $gg_lat;
//        $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
//        $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
//        $bd_lon = $z * cos($theta) + 0.0065;
//        $bd_lat = $z * sin($theta) + 0.006;
//        // 保留小数点后六位
//        $data['lng'] = round($bd_lon, 6);
//        $data['lat'] = round($bd_lat, 6);


        $url = "http://api.map.baidu.com/geocoder/v2/?ak=CkPBYsH41GvvVl1tvSRBEu0MhM1sGdQ9&location={$gg_lat},{$gg_lon}&output=json&pois=1";

        $return = getRequest($url);

        $return = json_decode($return, true);

        $data['address'] = [];

        foreach ($return as $key => $value) {
            $data['address'] = $value['pois'][0]['addr'];
            $data['name'] = $value['pois'][0]['name'];
        }
        if (empty($data['address'])) {
            $data['address'] = '广东省深圳市龙岗区';
            $data['name'] = '龙城广场';
//            $data['lng'] = 114.255553;
//            $data['lat'] = 22.724551;
        }

        return $data;
    }

}